
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

    public class SimpleCalculator extends JFrame implements ActionListener {

        private JTextField display;
        private double num1 = 0, num2 = 0, result = 0;
        private char operator = ' ';
        private boolean isOperand = true;

        public SimpleCalculator() {
            setTitle("Simple Calculator");
            setSize(300, 400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            display = new JTextField();
            display.setEditable(false);
            display.setHorizontalAlignment(JTextField.RIGHT);
            add(display, BorderLayout.NORTH);

            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(4, 4));

            String[] buttons = {
                    "7", "8", "9", "/",
                    "4", "5", "6", "*",
                    "1", "2", "3", "-",
                    "0", ".", "=", "+"
            };

            for (String btnText : buttons) {
                JButton button = new JButton(btnText);
                button.addActionListener(this);
                panel.add(button);
            }

            add(panel, BorderLayout.CENTER);
            setVisible(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String btnText = e.getActionCommand();

            if (btnText.equals("=")) {
                switch (operator) {
                    case '+':
                        result = num1 + num2;
                        break;
                    case '-':
                        result = num1 - num2;
                        break;
                    case '*':
                        result = num1 * num2;
                        break;
                    case '/':
                        if (num2 != 0) {
                            result = num1 / num2;
                        } else {
                            display.setText("Error: Division by zero");
                            return;
                        }
                        break;
                }
                display.setText(String.valueOf(result));
                num1 = result; // Reset num1 for next calculation
                num2 = 0;
                operator = ' ';
                isOperand = true;
            } else if (Character.isDigit(btnText.charAt(0)) || btnText.equals(".")) {
                if (isOperand) {
                    display.setText(""); // Clear display if operand is new
                    num1 = 0;
                    isOperand = false;
                }
                if (btnText.equals(".")) {
                    if (!display.getText().contains(".")) {
                        display.setText(display.getText() + ".");
                    }
                } else {
                    display.setText(display.getText() + btnText);
                }
                num1 = Double.parseDouble(display.getText());
            } else {
                if (!isOperand) {
                    num2 = Double.parseDouble(display.getText());
                    operator = btnText.charAt(0);
                    isOperand = true;
                    display.setText(""); // Clear display for next operand
                }
            }
        }

        public static void main(String[] args) {
            new SimpleCalculator();
        }
    }

